import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TESTT {
	
	private static final byte SCRAMBLE_MASK = (byte) 0xFF;
	public static void main(String[] args) {
		
		
		
		/*String formatStr = "%02d";
        String formatAns = String.format(formatStr, 4);
        System.out.println("數字補零:"+formatAns);*/
		Date today = new Date();
		int year = 1900+today.getYear();
		int month = today.getMonth()+1;
		int day = today.getDate();
		String td = year+"-"+String.format("%02d", month)+"-"+String.format("%02d", day);
		System.out.println("原day:"+day);
		
		if( day==1 ) {
			if( month==1 ) {
				year--;
				month=12;
				day=31;
			} else{
				month=month-1;
				Calendar mycal = new GregorianCalendar(year, (month-1), 1);
				day = mycal.getActualMaximum(Calendar.DAY_OF_MONTH); // 28
			}
		} else {
			day = day-1;
		}
		
		System.out.println("today:"+today);
		System.out.println("year:"+year);
		System.out.println("month:"+month);
		System.out.println("day:"+day);
		
	}
	
	
	static public String chtScramble(String input) {

        String output = "";

        try {

            byte[] input_bytes = input.getBytes("MS950");
            byte[] output_bytes = new byte[input_bytes.length];

            for(int i = 0 ; i < input_bytes.length ; i++){

            	byte b = (byte) (input_bytes[i] ^ SCRAMBLE_MASK);
                output_bytes[i] = b;
            }

            output = new String(output_bytes, "8859_1");
        }
        catch (Exception e){
            StackTraceElement[] stes = e.getStackTrace();
            String resultDesc = e.toString() + " at:";
            int j = (stes.length > 20) ? 20 : stes.length;
            for (int i = 0; i < j; i++) {
                resultDesc += ("\n" + stes[i].toString());
            }
        }

        return output;
    }

    /**
     *@param input 加密字串
     *
     *@return 原始字串
     */
    static public String chtDescramble(String input) {

        String output = "";

        try {

            byte[] input_bytes = input.getBytes("8859_1");
            byte[] output_bytes = new byte[input_bytes.length];

            for(int i = 0 ; i < input_bytes.length ; i++){

            	byte b = (byte) (input_bytes[i] ^ SCRAMBLE_MASK);
                output_bytes[i] = b;
            }

            output = new String(output_bytes, "MS950");
        }
        catch (Exception e){
            StackTraceElement[] stes = e.getStackTrace();
            String resultDesc = e.toString() + " at:";
            int j = (stes.length > 20) ? 20 : stes.length;
            for (int i = 0; i < j; i++) {
                resultDesc += ("\n" + stes[i].toString());
            }
        }

        return output;
    }
}
